<?php
/**
 * 开奖数据模型类
 *
 *
 *
 *
 * @copyright  Copyright (c) 2014-2015 TuntronKJ Inc. (http://www.tuntron.com)
 * @license    http://www.tuntron.com
 * @link       http://www.tuntron.com
 * @since      File available since Release v1.1
 */
defined('InTuntronKJ') or exit('Access Invalid!');

class data_classModel{
	/**
	 * 添加开奖数据
	 *
	 * @param array $param 参数内容
	 * @return bool 布尔类型的返回结果
	 */
	public function data_add($param){
		if (empty($param)){
			return false;
		}
		if (is_array($param)){
			$tmp = array();
			foreach ($param as $k => $v){
				$tmp[$k] = $v;
			}
			$result = Db::insert('data',$tmp);
			return $result;
		}else {
			return false;
		}
	}
  
	/**
	 * 删除一条开奖数据
	 *
	 * @param array $param 参数内容
	 * @return bool 布尔类型的返回结果
	 */
	public function data_del($id){
		$where  = "where id = '$id'";
		$result = Db::delete("data",$where);
		return $result;
	}
	
	/**
	 * 获取开奖数据列表
	 *
	 * @param array $condition 查询条件
	 * @param obj $page 分页对象
	 * @return array 二维数组
	 */
	public function getDataList($condition=array(), $page='', $orderby=''){
		$param	= array();
		$param['table']	= 'data';
		$param['where']	= $this->getCondition($condition);
	    if($orderby == ''){
			$param['order'] = 'id desc';
		}else{
			$param['order'] = $orderby;
		}
		return Db::select($param,$page);
	}

	/**
	 * 根据条件查询多条记录
	 *
	 * @param array $condition 查询条件
	 * @param obj $page 分页对象
	 * @return array 二维数组
	 */
	public function getList($condition=array(), $page='', $limit='', $orderby=''){
		$param	= array();
		$param['table']	= 'data';
		$param['field'] = $condition['field']?$condition['field']:'*';
		$param['where']	= $this->getCondition($condition);
		if($orderby == ''){
			$param['order'] = 'id desc';
		}else{
			$param['order'] = $orderby;
		}
		$param['limit'] = $limit;
		return Db::select($param,$page);
	}

	//获取延迟时间
	public function getTypeFtime($type){
		
		if($type){
				//$Ftime=$this->getValue("select data_ftime from {$this->prename}type where id = ? ", $type);
			}
			if(!$Ftime) $Ftime=0;
			return intval($Ftime);
	 }

	 
	//获取当期时间
	public function getGameActionTime($type,$old=0){
		$actionNo=$this->getGameNo($type);
		
		if($type==1 && $actionNo['actionTime']=='00:00'){
			$actionTime=strtotime($actionNo['actionTime'])+24*3600;
		}else{
			$actionTime=strtotime($actionNo['actionTime']);
		}
		if(!$actionTime) $actionTime=$old;
		return $actionTime;
	}/////
	
	//获取当期封单时间
	public function getGameStopTime($type,$old=0){
		$actionNo=$this->getGameNo($type);
		
		if($type==1 && $actionNo['actionTime']=='00:00'){
			$stopTime=strtotime($actionNo['stopTime'])+24*3600;
		}else{
			$stopTime=strtotime($actionNo['stopTime']);
		}
		if(!$stopTime) $stopTime=$old;
		return $stopTime;
	}/////
	
	//获取当期期数
	public function getGameActionNo($type){
		$actionNo=$this->getGameNo($type);
		return $actionNo['actionNo'];
	}/////
	//随机函数
	public function randomkeys($length)
	{
	 $key = "";
	 $pattern='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	 $pattern1='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	 $pattern2='0123456789';
	 for($i=0;$i<$length;$i++)
	 {
	   $key .= $pattern{mt_rand(0,35)};
	 }

	 return $key;
	}

	//输出单号
	function formatwords($str){
		
	if($str){
		$len=strlen($str);  
		for($i=0;$i<$len;$i++){
			echo "<i>".substr($str, $i, 1)."</i>";
			
		}
	 }
	}/////


	/**
	 * 读取将要开奖期号
	 *
	 * @params $typeid	查询条件
	 * @params $time		时间，如果没有，当默认当前时间
	 * @params $flag		如果为true，则返回最近过去的一期（一般是最近开奖的一期），如果为flase，则是将要开奖的一期
	 */
	public function getGameNo($typeid, $time=null){

		if($time===null) $time=time();
		$atime=date('H:i:s', $time);
		$param['table']	= 'data_time';
		$param['where']	= "type='".$typeid."' and actionTime > '".$atime."'";
		$param['order']	= 'actionTime asc';
		$param['limit']	= 1;
		$result	= Db::select($param);

		if(!is_array($result) and !is_array($result[0])){
			$param['where']	= "type='".$typeid."'";
			$result	= Db::select($param);
			$time=$time+24*3600;
		}

		/*函数处理
		$types=$this->getTypes();
		if(($fun=$types[$type]['onGetNoed']) && method_exists($this, $fun)){
			$this->$fun($return['actionNo'], $return['actionTime'], $time);
		}
		*/
		return $result[0];
	}




	/**
	 * 根据id查询一条记录
	 *
	 * @param int $id 广告id
	 * @return array 一维数组
	 */
	public function getOneById($id){
		$param	= array();
		$param['table']	= 'data';
		$param['field']	= 'id';
		$param['value']	= $id;
		return Db::getRow($param);
	}

	/**
	 * 更新记录
	 *
	 * @param array $param 更新内容
	 * @return bool
	 */
	public function update($param){
		return Db::update('data',$param,"id='{$param['id']}'");
	}
   
	/**
	 * 构造查询条件
	 *
	 * @param array $condition
	 * @return string
	 */
	private function getCondition($condition = array()){
		$return	= '';
		$time   = time();
		if($condition['type'] != ''){
			$return	.= " and type='".$condition['type']."'";
		}
		if($condition['time'] != ''){
			$return	.= " and time='".$condition['time']."'";
		}
		if($condition['number'] != ''){
			$return	.= " and number ='".$condition['number']."'";
		}
		if ($condition['data'] != '') {
			$return .= " and data='".$condition['data']."'";
		}

		return $return;
	}

	public function delapcache($id){
		if (!is_numeric($id)) return ;
		$filename = BASE_DATA_PATH.DS.'cache/data/'.$id.'.php';
		if(file_exists($filename)) @unlink($filename);
		return true;
	}

	/**
	 * 生成开奖数据缓存
	 *
	 * @param int $id
	 */
	public function makeDataCache($id = null){
		if (empty($id)) return '';
		$model = model();
		$info = $model->table('data')->where(array('id'=>$id))->find();
		write_file(BASE_DATA_PATH.'/cache/data/'.$id.'.php',$info);
	}
}